//
//  TitleView.m
//  BaiTuan
//
//  Created by huiwenjiaoyu on 16/1/4.
//  Copyright © 2016年 YR. All rights reserved.
//

#import "TitleView.h"

@implementation TitleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createLabels];
        
    }
    return self;
}

#pragma mark - 创建视图

- (void)createLabels
{
    self.title = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 20)];
    self.title.font = [UIFont systemFontOfSize:14];
    self.title.textColor = [UIColor whiteColor];
    self.title.backgroundColor = [UIColor clearColor];
    [self addSubview:self.title];
    
    self.saleNum = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width - 80, 0, 70, 20)];
    self.saleNum.textAlignment = NSTextAlignmentRight;
    self.saleNum.font = [UIFont systemFontOfSize:14];
    self.saleNum.textColor = [UIColor whiteColor];
    self.saleNum.backgroundColor = [UIColor clearColor];
    [self addSubview:self.saleNum];
    
    self.desLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.title.frame) + 20, self.frame.size.width, 0)];
    self.desLabel.font = [UIFont systemFontOfSize:14];
    self.desLabel.textColor = [UIColor whiteColor];
    self.desLabel.numberOfLines = 0;
    self.desLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:self.desLabel];
}

#pragma mark - 更新数据

- (void)setInfo:(NSDictionary *)info
{
    if (_info != info) {
        _info = info;
        
        self.title.text = _info[@"title"];
        self.saleNum.text = [NSString stringWithFormat:@"%@",_info[@"sale_num"]];
        self.desLabel.text = _info[@"description"];
        [self caculateViewHight];
    }
}

#pragma  mark - 计算视图高度

- (void)caculateViewHight
{
    CGRect frame;
    frame = [self.desLabel.text boundingRectWithSize:CGSizeMake(kScreenWidth, 1000)
                                             options:NSStringDrawingUsesLineFragmentOrigin
                                          attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]}
                                             context:nil];
    CGRect desLabelFrame = self.desLabel.frame;
    desLabelFrame.size.height = frame.size.height;
    self.desLabel.frame = desLabelFrame;
    
    CGRect viewFrame = self.frame;
    viewFrame.size.height = CGRectGetMaxY(self.desLabel.frame) + 10;
    self.frame = viewFrame;
}

@end
